package model

import (
	"gorm.io/gorm"
	"smart-health-platforms/config"
	"time"
	"user_srv/user"
)

type MemberUserLevel struct {
	Id         int32          `gorm:"column:id;type:int;primaryKey;not null;" json:"id"`
	UserId     int32          `gorm:"column:user_id;type:int;comment:用户uid;not null;default:0;" json:"user_id"`              // 用户uid
	LevelId    int32          `gorm:"column:level_id;type:int;comment:等级vip;not null;default:0;" json:"level_id"`            // 等级vip
	ExpireDate uint64         `gorm:"column:expire_date;type:bigint UNSIGNED;comment:到期日期;default:NULL;" json:"expire_date"` // 到期日期
	Points     int32          `gorm:"column:points;type:int;comment:积分;not null;default:0;" json:"points"`                   // 积分
	CreatedAt  time.Time      `gorm:"column:created_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"created_at"`
	UpdatedAt  time.Time      `gorm:"column:updated_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"updated_at"`
	DeletedAt  gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);default:NULL;" json:"deleted_at"`
}

func (m *MemberUserLevel) TableName() string { //会员用户等级
	return "member_user_level"
}
func (m *MemberUserLevel) GetUserLevel() error { //获取用户等级
	return config.DB.Model(&MemberUserLevel{}).Where("user_id = ?", m.UserId).First(&m).Error
}
func (m *MemberUserLevel) PointsIncrease(num int32) error { //积分增加
	return config.DB.Model(&MemberUserLevel{}).Where("user_id = ?", m.UserId).Update("points", gorm.Expr("points + ?", num)).Error
}
func (m *MemberUserLevel) UpdateMemberUserLevel(num1 int) error { //更新会员用户等级
	return config.DB.Model(&MemberUserLevel{}).Where("user_id = ?", m.UserId).Update("expire_date", gorm.Expr("expire_date + ?", num1)).Error
}
func (m *MemberUserLevel) ModifyMembershipLevel(num3 int) error { //修改会员等级
	return config.DB.Model(&MemberUserLevel{}).Where("user_id = ?", m.UserId).Update("level_id", num3).Error
}
func (m *MemberUserLevel) MemberUserList() (list []*user.MembershipUserList, err error) {
	err = config.DB.Model(&MemberUserLevel{}).
		Select("users.nick_name,users.image_cur,users.gender,users.age,membership_levels.*").
		Joins("left join users on users.id = member_user_level.user_id").
		Joins("left join membership_levels on membership_levels.id = member_user_level.id").
		Find(&list).Error
	return
}
